package com.taxx.controller;

import com.taxx.common.Result;
import com.taxx.entity.SysUser;
import com.taxx.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.taxx.entity.SysRole;

/**
 * 系统用户管理控制器
 */
@RestController
@RequestMapping("/users")
@CrossOrigin
public class SysUserController {
    
    @Autowired
    private SysUserService userService;
    
    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public Result<List<SysUser>> getUserList() {
        try {
            List<SysUser> users = userService.getUserList();
            return Result.success("获取用户列表成功", users);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取用户信息
     */
    @GetMapping("/{id}")
    public Result<SysUser> getUserById(@PathVariable Long id) {
        try {
            SysUser user = userService.getUserInfo(id);
            if (user == null) {
                return Result.notFound("用户不存在");
            }
            return Result.success("获取用户信息成功", user);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 创建用户
     */
    @PostMapping
    public Result<Void> createUser(@Validated @RequestBody SysUser user, 
                                   @RequestParam(required = false) List<Long> roleIds) {
        try {
            boolean success = userService.createUser(user, roleIds);
            if (success) {
                return Result.success();
            } else {
                return Result.error("用户创建失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新用户
     */
    @PutMapping("/{id}")
    public Result<Void> updateUser(@PathVariable Long id, 
                                   @Validated @RequestBody SysUser user,
                                   @RequestParam(required = false) List<Long> roleIds) {
        try {
            user.setId(id);
            boolean success = userService.updateUser(user, roleIds);
            if (success) {
                return Result.success();
            } else {
                return Result.error("用户更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        try {
            boolean success = userService.deleteUser(id);
            if (success) {
                return Result.success();
            } else {
                return Result.error("用户删除失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 修改密码
     */
    @PostMapping("/{id}/change-password")
    public Result<Void> changePassword(@PathVariable Long id,
                                      @RequestParam String oldPassword,
                                      @RequestParam String newPassword) {
        try {
            boolean success = userService.changePassword(id, oldPassword, newPassword);
            if (success) {
                return Result.success();
            } else {
                return Result.error("密码修改失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 重置密码
     */
    @PostMapping("/{id}/reset-password")
    public Result<Void> resetPassword(@PathVariable Long id,
                                     @RequestParam String newPassword) {
        try {
            boolean success = userService.resetPassword(id, newPassword);
            if (success) {
                return Result.success();
            } else {
                return Result.error("密码重置失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新用户状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateUserStatus(@PathVariable Long id,
                                        @RequestParam Integer status) {
        try {
            boolean success = userService.updateUserStatus(id, status);
            if (success) {
                return Result.success();
            } else {
                return Result.error("用户状态更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分配角色给用户
     */
    @PostMapping("/{id}/roles")
    public Result<Void> assignRoles(@PathVariable Long id,
                                    @RequestBody RoleAssignmentRequest request) {
        try {
            boolean success = userService.assignRoles(id, request.getRoleIds());
            if (success) {
                return Result.success();
            } else {
                return Result.error("角色分配失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户的角色列表
     */
    @GetMapping("/{id}/roles")
    public Result<List<SysRole>> getUserRoles(@PathVariable Long id) {
        try {
            List<SysRole> roles = userService.getUserRoles(id);
            return Result.success("获取用户角色成功", roles);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 移除用户的角色
     */
    @DeleteMapping("/{id}/roles/{roleId}")
    public Result<Void> removeUserRole(@PathVariable Long id,
                                       @PathVariable Long roleId) {
        try {
            boolean success = userService.removeUserRole(id, roleId);
            if (success) {
                return Result.success();
            } else {
                return Result.error("角色移除失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量分配角色
     */
    @PostMapping("/batch-assign-roles")
    public Result<Void> batchAssignRoles(@RequestBody BatchRoleAssignmentRequest request) {
        try {
            boolean success = userService.batchAssignRoles(request.getUserIds(), request.getRoleIds());
            if (success) {
                return Result.success();
            } else {
                return Result.error("批量角色分配失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 角色分配请求
     */
    public static class RoleAssignmentRequest {
        private List<Long> roleIds;

        public List<Long> getRoleIds() {
            return roleIds;
        }

        public void setRoleIds(List<Long> roleIds) {
            this.roleIds = roleIds;
        }
    }

    /**
     * 批量角色分配请求
     */
    public static class BatchRoleAssignmentRequest {
        private List<Long> userIds;
        private List<Long> roleIds;

        public List<Long> getUserIds() {
            return userIds;
        }

        public void setUserIds(List<Long> userIds) {
            this.userIds = userIds;
        }

        public List<Long> getRoleIds() {
            return roleIds;
        }

        public void setRoleIds(List<Long> roleIds) {
            this.roleIds = roleIds;
        }
    }
}

